home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 561 / proltu.tor < prev    next >
Encoding:
Text File  |  1991-09-18  |  28.1 KB  |  681 lines

  1.  
  2.                      A.D.A PROLOG Documentation Version 1.91
  3.                   for the Educational and Public Domain Versions
  4.  
  5.                                   May 28, 1986
  6.  
  7.               Copyright Robert Morein and Automata Design Associates
  8.  
  9.                                  1570 Arran Way
  10.                                Dresher, Pa. 19025
  11.  
  12.                                  (215)-646-4894
  13.  
  14.  
  15.                                 Copyright Notice
  16.  
  17.              The  public domain PD PROLOG system has been contributed  to 
  18.         the  public domain for unrestricted use with one  exception:  the 
  19.         object  code  may not be  disassembled  or  modified.  Electronic 
  20.         bulletin  boards  and SIG groups are urged to aid in giving  this 
  21.         software the widest possible distribution.
  22.  
  23.              This documentation may be reproduced freely,  but it may not 
  24.         be  included in any other documentation without the permission of 
  25.         the author.
  26.  
  27.                                   Introduction
  28.                 
  29.         We  hope  that you'll get some fun out of this  PROLOG.  It  will 
  30.         afford  you exposure to THE fifth generation language at the cost 
  31.         only  of  some  intellectual  effort.  The  motive  is  perfectly 
  32.         explicable:  We  want you to think of Automata Design  Associates 
  33.         for  fifth  generation  software.  It also gives us a  nice  warm 
  34.         feeling.
  35.  
  36.  
  37.                                  Prolog Tutorial          
  38.  
  39.  
  40.                                   Introduction
  41.  
  42.  
  43.  
  44.  
  45.              Probably  you  have heard of the language PROLOG within  the 
  46.         last year or so. You probably wondered the following things:
  47.  
  48.         1) What does the name stand for? Names of computer languages are 
  49.         almost always acronyms.
  50.  
  51.         2) What is it good for?
  52.  
  53.         3) Why now?
  54.  
  55.         4) Can I get a copy to play with?
  56.  
  57.              Congratulations! You obviously know the answer to the fourth 
  58.         question. We now respond to the other three.
  59.              
  60.         1)  The  name  stands for "programming in logic." This  we  shall 
  61.         elaborate on in depth later on.
  62.  
  63.         2) PROLOG is good for writing question answering systems.  It  is 
  64.         also   good   for  writing  programs  that  perform   complicated 
  65.         strategies  that  compute the best or worst way to  accomplish  a 
  66.         task, or avoid an undesirable result.
  67.  
  68.         3) PROLOG was virtually unknown in this country until researchers 
  69.         in  Japan announced that it was to be the core language  of  that 
  70.         country's fifth generation computer project.  This is the project 
  71.         with  which  Japan hopes to achieve a domainant position  in  the 
  72.         world information industry of the 1990's. 
  73.  
  74.              PROLOG  is  one of the most unusual computer languages  ever 
  75.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  76.         BASIC.  The facilities complement,  rather than replace those  of 
  77.         conventional  languages.  Although  it  has great  potential  for 
  78.         database  work,  it  has  nothing in  common  with  the  database 
  79.         languages used on microcomputers.
  80.  
  81.              Perhaps  the  best point to make is that while  conventional 
  82.         languages are prescriptive, PROLOG is descriptive. A statement in 
  83.         a conventional language might read:
  84.  
  85.              if( car_wheels = TRUE ) then
  86.                begin
  87.                  (some sort of procedure)
  88.                  X = X + 1;
  89.                end 
  90.  
  91.  
  92.  
  93.         A statment in PROLOG could just be a statment of fact about  cars 
  94.         and wheels. There are many relationships that hold. For instance,
  95.  
  96.              has( car, wheels ).
  97.  
  98.              has( car, quant(wheels, four) ).
  99.  
  100.              round( wheels ).
  101.  
  102.         Each  of  these statments is an independent fact  relating  cars, 
  103.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  104.         independent, they can be put into a PROLOG program by programmers 
  105.         working separately. The man who is a specialist on car bodies can 
  106.         say  his thing,  the wheel specialist can have his say,  and  the 
  107.         participants can work with relative independence. And this brings 
  108.         to light a major advantage of PROLOG:
  109.  
  110.  
  111.                              PARALLEL PROGRAMMING!!!
  112.                             
  113.  
  114.         With  conventional  programming languages projects can  still  be 
  115.         "chunked",  or  divided between programmers.  But efficiency on a 
  116.         team  project  drops  drastically below that  of  the  individual 
  117.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  118.         participants    grows   the   need   for   communication    grows 
  119.         geometrically. The time spent communicating can exceed that spent 
  120.         programming! 
  121.              Although   PROLOG   does   not  eliminate   the   need   for 
  122.         task  coordination,  the problem is considerably  simplified.  It 
  123.         also provides the ability to answer questions in a "ready to  eat 
  124.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  125.         statements about cars and wheels previously given,  could you ask 
  126.         it the following question?   
  127.  
  128.                        
  129.               has( car, X ), round( X ).
  130.  
  131.              Does  a  car  have anything which  is  round?  The  question 
  132.         instructs the PROLOG interpreter to consider all the objects that 
  133.         it  knows are possessed by a car and find those which are  round. 
  134.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  135.         of a smart database searcher.  It can not only find the facts but 
  136.         selectively find them and interpret them.
  137.  
  138.              Consider the problem of a fault tree, as exemplified by this 
  139.         abbreviated one:
  140.  
  141.  
  142.  
  143.         {Car won't start}
  144.              | 
  145.              | 
  146.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  147.                 (Yes)                                       v
  148.                  |                                  {Check battery}
  149.                  |
  150.         [Smell gasoline](yes) --> {Try full throttle cranking}
  151.                  |                       (failure)
  152.         /--------/                           |
  153.  
  154.                             (details omitted)
  155.  
  156.  
  157.  
  158.              The fault tree is easily programmed in BASIC. Later we shall 
  159.         show  that  PROLOG supplies a superior replacement for the  fault 
  160.         tree.  Though the tree is capable of diagnosing only the  problem 
  161.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  162.         appropriate  tree from facts and rules you have provided.  PROLOG 
  163.         is not limited to answering one specific question.  Given  enough 
  164.         information,  it  will attempt to find all deductive solutions to 
  165.         any problem.
  166.  
  167.                                   PROLOG PRIMER
  168.  
  169.         I.                       Rules and Facts     
  170.  
  171.  
  172.  
  173.              This  is  where you should start if you know  nothing  about 
  174.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  175.  
  176.         1)   has( car, wheels ).
  177.  
  178.         This  statement  is a "fact.  The word "has" in this statment  is 
  179.         known  either  as a functor or predicate.  It is a name  for  the 
  180.         relationship  within the parenthesis.  It implies that a car  has 
  181.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  182.         arbitrary and established by you. You could just as easily say:
  183.  
  184.         2)   has( wheels, car ).
  185.  
  186.         and if you wrote this way consistently,  all would be  well.  The 
  187.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  188.         "car" are constants. 
  189.              
  190.         A   database   of  facts  can  illustrate  the   data   retrieval 
  191.         capabilities of PROLOG. For instance:
  192.  
  193.         3)   has( car, wheels ).
  194.              has( car, frame ).
  195.              has( car, windshield ).
  196.              has( car, engine ).
  197.  
  198.         You could then ask PROLOG the question:
  199.  
  200.         4)   has( car, Part ).
  201.  
  202.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  203.         will make Part equal to whatever constant is required to make the 
  204.         question match one of the facts in the database. Thus PROLOG will 
  205.         respond:
  206.  
  207.              Part = wheels.
  208.              
  209.              More?(Y/N):
  210.  
  211.         If you type "y" the next answer will appear:
  212.  
  213.              Part = frame.
  214.  
  215.              More?(Y/N):
  216.  
  217.         If you continue, PROLOG will produce the answers Part = windshield 
  218.         and Part = engine. Finally, you will see:
  219.  
  220.              More?(Y/N):y
  221.  
  222.              No.
  223.          
  224.         indicating that PROLOG has exhausted the database.  Incidentally, 
  225.         when  a  variable is set equal to a constant or  other  variable, 
  226.         it is said to be instantiated to that object.
  227.  
  228.              Notice  that  PROLOG searches the database forwards  and  in 
  229.         this case,  from the beginning.  The forward search path is built 
  230.         into PROLOG and cannot be changed. An author of a program written 
  231.         in  a  prescriptive language is quite conscious of the  order  of 
  232.         execution  of  his program,  while in PROLOG it is  not  directly 
  233.         under his control.
  234.              
  235.              The other major element is the rule which is a fact which is 
  236.         conditionally true. In logic this is called a Horn clause: 
  237.  
  238.  
  239.         5)   has( X, wheels ) :- iscar( X ).
  240.  
  241.         The  fact iscar( car ) and the above rule are equivalent to
  242.  
  243.         6)   has( car, wheels).
  244.  
  245.         The  symbol :- is the "rule sign." The expression on the left  of 
  246.         :-is the "head" and on the right is the body.  The variable X has 
  247.         scope  of the rule,  which means that it has meaning only  within 
  248.         the rule.  For instance,  we could have two rules in the database 
  249.         using identically named variables.
  250.  
  251.  
  252.         7)   has( X,  transportation ) :- 
  253.                            has( X,  car ), has( license, X ).
  254.  
  255.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  256.  
  257.         The  variables  X in the two expressions are completely  distinct 
  258.         and have nothing to do with each other.
  259.  
  260.         The comma between has( X, car ) and has( license, X ) means "and" 
  261.         or logical conjuction.  The rule will not be true unless both the 
  262.         clauses has(X, car) and has( license, X ) are true.
  263.  
  264.  
  265.              On the other hand if there is a rule
  266.              
  267.         9)   has( license, X ) :- passedexam( X ).
  268.  
  269.         consider what PROLOG will do in response to the question:
  270.  
  271.         10)  has( harry, transportation ).
  272.  
  273.         (Notice  that  harry has not been capitalized because we  do  not 
  274.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  275.         enclosed in single quotes.)
  276.  
  277.              It  will scan the database and use (7),  in which X will  be 
  278.         instantiated to harry. The rule generates two new questions:
  279.  
  280.         11)  has( harry, car ).
  281.  
  282.         12)  has( license, harry ).
  283.  
  284.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  285.         satisfied and the database is scanned for a match to (12). PROLOG 
  286.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  287.         question is now posed:
  288.  
  289.         13)  passedexam( harry ).
  290.  
  291.              If there is a fact:
  292.  
  293.              passedexam( harry ).
  294.  
  295.         in  the database then all is well and harry  has  transportation. 
  296.         If there is not, then PROLOG will succinctly tell you:
  297.  
  298.              No.
  299.  
  300.         But  suppose Harry has money and can hire a chauffer as any  good 
  301.         programmer  can.  That  could be made part of the program in  the 
  302.         following way.
  303.  
  304.              The rule which PROLOG tried to use was:
  305.  
  306.         14)  has( X,  transportation ) :- 
  307.                            has( X,  car ), has( license, X ).
  308.  
  309.         At any point following it there could be included another rule:
  310.  
  311.         15)  has( X, transportation ) :- has( X, money ).
  312.  
  313.         or simply the bald fact:
  314.  
  315.         16)  has( harry, transportation ).
  316.  
  317.              These  additional  rules  or  facts would  be  used  in  two 
  318.         circumstances.  If at any point a rule does not yield a solution, 
  319.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  320.         applicable  one.  This process is known as "backtracking  search" 
  321.         and can be quite time consuming.
  322.  
  323.  
  324.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  325.         search  for an additional distinct solution.  It will attempt  to 
  326.         find an alternate rule or fact for the last rule or fact used. If 
  327.         that  fails,  it  will back up to the antecedent rule and try  to 
  328.         find  an alternate antecedent.  And it will continue to  back  up 
  329.         until  it  arrives at the question you asked,  at which point  it 
  330.         will say
  331.  
  332.              No.
  333.  
  334.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  335.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  336.         question (16).
  337.  
  338.  
  339.  
  340.  
  341.         II.                          Grammar
  342.  
  343.              It is a boring subject, but it must be discussed. All PROLOG 
  344.         statements are composed of valid terms, possibly a rule sign (":-
  345.         "),  commas representing conjunction ("and"), and a period at the 
  346.         very end.
  347.              A term is a structure, constant, variable, or number.
  348.          
  349.              What is a structure? It is a kind of grouping:
  350.  
  351.              1) Structures consist of a functor, and a set of objects or
  352.                 structures in parenthesis.
  353.  
  354.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  355.                 which we have not discussed yet.
  356.  
  357.              3)  A  constant or functor must be a string of  letters  and 
  358.                 numbers, beginning with a lower case letter, unless
  359.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  360.                 pardner'  ),  in  which  case you are  freed  from  these 
  361.                 restrictions.
  362.              4) A  variable  must be a string of  letters  and  numbers 
  363.                 beginning with a capital letter.
  364.              
  365.              5) A  functor  may optionally have  arguments  enclosed  in 
  366.                 parenthesis , as in: hascar( X ) or hascar. 
  367.  
  368.         An example:  "has( X, transportation )." is a structure.
  369.  
  370.         III.                     Input / Output      
  371.  
  372.              You   now   know  enough  to  write  simple  databases   and 
  373.         interrogate   them  profitably.   But  before  we  examine   more 
  374.         sophisticated  examples,  it  will be necessary to add input  and 
  375.         output to the language. There are built in functions which appear 
  376.         as rules which are satisfied once. Thus the statment:
  377.  
  378.              write( 'Hello world.' ).
  379.  
  380.         can be included on the right side of a rule:
  381.  
  382.  
  383.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  384.         can  also write "write( X )" where X is some variable.  Note that 
  385.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  386.         which denote a constant, are required. Double quotes would denote 
  387.         a list, which is another thing entirely.
  388.  
  389.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  390.         function  "write"  is  executed and the message  printed  to  the 
  391.         screen.
  392.              The  builtin  read( X ) reads a "structure" that  you  input 
  393.         from the keyboard. More formally, we have
  394.  
  395.              read( <variable> or <constant> )
  396.              write( <variable> or <constant> )
  397.  
  398.         If you write read( Input ),  then the variable "keyboard" will be 
  399.         assigned to whatever is typed at the keyboard,  provided that the 
  400.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  401.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  402.         is a constant,  and the user at the keyboard did not type exactly 
  403.         "baloney." 
  404.  
  405.         When you input a structure in response to a "read" statement,  be 
  406.         sure to end it with a period and an <ENTER>. 
  407.  
  408.              There  is  a convenient way of putting the cursor on  a  new 
  409.         line. This is the builtin "nl". For example:
  410.  
  411.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  412.  
  413.         would result in:
  414.  
  415.              line 1
  416.              line 2
  417.  
  418.              There  is  also a primitive form of input/output for  single 
  419.         characters. It will be discussed later.
  420.  
  421.  
  422.         IV.                   A Fault Tree Example
  423.  
  424.              Consider the "won't start" fault tree for an automobile:
  425.  
  426.         {Car won't start}
  427.              | 
  428.              | 
  429.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  430.                 (Yes)                                       v
  431.                  |                                  {Check battery}
  432.                  |
  433.         [Smell gasoline](yes) --> {Try full throttle cranking}
  434.                  |                       (failure)
  435.         /--------/                           |
  436.         |           /------------------------/ 
  437.         |           |                       
  438.         |           |
  439.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  440.         |          (no)
  441.         |           |
  442.         |           |
  443.         |  [Check for defective carburator](yes)--\
  444.         |          (no)                           v
  445.         |                                {Repair carburator}
  446.         \----\
  447.              |
  448.              |
  449.         [Is spark present](no)-->[Do points open and close](no)-\
  450.              |                             (yes)                v
  451.         /----/                               |          {Adjust points}
  452.         |           /------------------------/
  453.         |           |
  454.         |  [Pull distributor wire, observe spark](blue)--\
  455.         |        (orange)                                v
  456.         |           |                           {Check plug wires & cap}
  457.         |           |
  458.         |  [Measure voltage on coil primary](not 12V)--\
  459.         |         (12V)                                v
  460.         |           |              {Check wiring, ballast resistor}
  461.         |           |
  462.         |  [Check condenser with ohmmeter](conducts)--\
  463.         |    (no conduction)                          v
  464.         |           |                         {Replace condenser}
  465.         |           |
  466.         |  [Open and close points](voltage not 0 - 12)--\
  467.         |   (voltage swings 0 - 12)                     v
  468.         |           |                         {Fix primary circuit}
  469.         |           |
  470.         |  {Consider hidden fault, swap components]
  471.         |
  472.         |
  473.         \-------{Call a tow truck!!}
  474.  
  475.              A PROLOG program to  implement this is simple. Each statment 
  476.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  477.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  478.         solution. 
  479.  
  480.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  481.                             affirm, nl,
  482.                             write( 'Check battery' ).
  483.  
  484.         'car wont start' :- write( 'Smell gasoline?' ), 
  485.                             affirm, nl,
  486.                             'fuel system'.
  487.  
  488.         'fuel system'    :- write( 'Try full throttle cranking' ).
  489.  
  490.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  491.                             affirm, nl,
  492.                             write( 'Replace fuel line.' ).
  493.  
  494.         'fuel system'    :- write( 'Check carburator' ).
  495.  
  496.         'car wont start' :- write( 'Is spark present?' ),
  497.                             not( affirm ), nl,
  498.                             'no spark'.
  499.  
  500.         'no spark'       :- write( 'Do points open and close?' ),
  501.                             not( affirm ), nl,
  502.                             write( 'Adjust or replace points.' ).
  503.  
  504.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  505.                             affirm,
  506.                             write( 'Check plug wires and cap.' ).
  507.  
  508.         'no spark'       :- write( 'What is the voltage on the primary
  509.                              of the coil: ' ), 
  510.                             read( Volts ), 
  511.                             Volts < 10,
  512.                             nl,
  513.                             write('Check wiring and ballast resistor.').
  514.  
  515.         'no spark'       :- write( 'Does the capacitor leak?' ),
  516.                             affirm,
  517.                             write( 'Replace the capacitor.' ).
  518.  
  519.         'no spark'       :- not( 'primary circuit' ).
  520.  
  521.         'primary circuit' 
  522.                          :- write( 'Open the  points.  Voltage  across 
  523.                               coil?:'), nl,
  524.                             read( Openvolts ), Openvolts < 1, 
  525.                             write(  'Close the points.  Voltage  across 
  526.                               coil?:'),
  527.                             read( Closevolts ), Closevolts > 10, nl,
  528.                             write( 'Primary circuit is OK.' ). 
  529.  
  530.         'no spark'       :- write( 'Consider a hidden fault. Swap
  531.                               cap, rotor,points,capacitor.' ).
  532.  
  533.  
  534.         'Car wont start' :- write( 'Get a tow truck!!' ).
  535.  
  536.  
  537.                                  --End program--
  538.  
  539.  
  540.              The  above  is  a  simple example of  an  expert  system.  A 
  541.         sophisticated  system would tell you exactly the method by  which 
  542.         it  has reached a conclusion.  It would communicate by a  "shell" 
  543.         program  written  in PROLOG which would accept a wider  range  of 
  544.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  545.         interpreter directly.
  546.  
  547.  
  548.         V.                            Lists               
  549.  
  550.              Consider  a  shopping list given you by your wife.  It is  a 
  551.         piece of paper with items written on it in an order that probably 
  552.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  553.         followed by carrots, hamburger, and finally a flea collar for the 
  554.         dog, if you can find one. In PROLOG such a list would be written:
  555.  
  556.         1)   [eggs, carrots, hamburger, fleacollar]
  557.  
  558.         The  order of a list is important so that eggs and carrots cannot 
  559.         be reversed and PROLOG be uncaring.
  560.  
  561.         Let us put the list in a structure:
  562.  
  563.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  564.  
  565.         Then  if you wished to isolate the head of the list you could ask 
  566.         the question:
  567.  
  568.              shopping( [ Mostimportant | Rest ] ).
  569.  
  570.         and PROLOG would respond:
  571.  
  572.              Mostimportant   =  eggs,   
  573.              Rest   =   [carrots,   hamburger, fleacollar].
  574.  
  575.         The vertical bar "|" is crucial here. It is the string extraction 
  576.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  577.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  578.         separate the head of the list from the rest, or tail.
  579.  
  580.  
  581.              You  may have gained the impression that PROLOG is a  rather 
  582.         static language capable of answering simple questions,  but it is 
  583.         far  more powerful than that.  The string extraction operator  is 
  584.         the  key.  It permits PROLOG to whittle a complex expression down 
  585.         to the bare remainder.  If the rules you have given it permit  it 
  586.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  587.         achieved. An example of this is the definition of "append."
  588.  
  589.              Let  us suppose you have not yet done yesterday's  shopping, 
  590.         let alone today's. You pull it out of your wallet and sootch tape 
  591.         it to the list your wife just gave you. Yesterday's list was:
  592.  
  593.              [tomatoes, onions, ketchup]
  594.  
  595.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  596.  
  597.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  598.  
  599.         To  take one list and to attach it to the tail of another list is 
  600.         to  "append"  the first to the second.  The PROLOG definition  of 
  601.         append is:
  602.  
  603.  
  604.         Rule1:     append( [], L, L ).
  605.  
  606.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  607.                       append( List1, List2, List3 ].
  608.  
  609.         The  general  scheme is this:  
  610.  
  611.         The definition consists of one rule and one fact.  The rule  will 
  612.         be used over and over again until what little is left matches the 
  613.         fact.  The [] stands for empty list,  which is like a bag without 
  614.         anything in it. This is an example of a recursive definition.
  615.              Suppose we ask:
  616.  
  617.              append( [a,b,c], [d,e,f], Whatgives ).
  618.  
  619.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives
  620. ).
  621.         2. Rule 2 is invoked again with arguments:
  622.              ( [b,c], [d,e,f], List3 ).
  623.         3. Rule 2 is invoked again with arguments:
  624.              ( [b], [d,e,f], List3 ).
  625.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  626.             matches. End.
  627.  
  628.         How does this cause a list to be constructed? The key is to watch 
  629.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  630.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  631.         2. Now lets trace this as rule two is successivly invoked: 
  632.  
  633.  
  634.                 Whatgives                                                
  635.                    |                                                     
  636.                    |                                                     
  637.                    |                                                     
  638.                    v                                                     
  639.         Rule2:  [X|List3] (List1 = [b,c])                                
  640.                  |  \                                                    
  641.                  |   \                                                   
  642.                  |    \                                                  
  643.                  v     \                                                 
  644.         Rule2:   a   [X'|List3'] (List1' = [c])                          
  645.                       |    \                                             
  646.                       |     \                                            
  647.                       |      \                                           
  648.                       v       \                                          
  649.         Rule2:        b     [X''|List3''] (List1'' = [], ie., empty set.)
  650.                               |    \                                     
  651.                               |     \                                    
  652.                               |      \                                   
  653.         Rule1:                c       L  ( in Rule1 = [d,e,f] )            
  654.  
  655.                                                                          
  656.         End.
  657.  
  658.  
  659.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  660.         2 never alters List2. It supplies it to whatever rule it invokes. 
  661.         So L in rule 1 is the original List2, or [a,b,c].
  662.  
  663.              This example would not have worked if the order of rules one 
  664.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  665.         attempts to use the the first rule encountered. You could imagine 
  666.         it as always reading your program from the top down in attempting 
  667.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  668.         an  unpleasant  thing  would  have happened  if  the  order  were 
  669.         reversed. The program would loop forever.
  670.              
  671.  
  672.  
  673.  
  674.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  675.         appetite. You should now purchase the book
  676.  
  677.              Programming In Prolog
  678.              W.F. Clocksin and C.S. Mellish
  679.              Springer - Verlag
  680.              Berlin,Heidelberg,New York. 1981,1984
  681. ə